import Vue from 'vue'
import Router from 'vue-router'
import Layout from '../views/layout/Layout'
import Index from '../views/home/Index'
import Login from '../views/login/Login'
import Agent from '../views/agent/Agent.vue'
import RoomCard from '../views/roomCard/RoomCard.vue'
import Record from '../views/roomCard/AddRecord.vue'
import Hall from '../views/game/Hall.vue'
import FeedBack from '../views/feedBack/FeedBack.vue'
import store from '../store/index.js'
import VipRoom from '../views/vip/VipRoom.vue'
import { initStore, clearStorage } from '../utils/index'
Vue.use(Router)

var router = new Router({
    routes: [{
            path: '/',
            name: 'layout',
            component: Layout,
            redirect: '/index',
            children: [
                { path: 'index', name: 'index', component: Index },
                { path: 'agent', name: 'agent', component: Agent },
                { path: 'roomCard', name: 'roomCard', component: RoomCard },
                { path: 'record', name: 'record', component: Record },
                { path: 'vipRoom', name: 'vipRoom', component: VipRoom },
                { path: 'hall', name: 'hall', component: Hall },
                { path: 'feedBack', name: 'feedBack', component: FeedBack },
            ]
        },
        { path: '/login', name: 'login', component: Login }

    ]
})

router.beforeEach((to, from, next) => {
    const token = store.getters.getToken || localStorage.getItem('token');

    if (to.path == '/login' || to.path == '/register') {
        next();
    } else {
        if (token) {
            //在这里把token存进store?
            if (!store.getters.getToken) {
                // 通过store.getters.getToken判定当前是非正常进入页面  比如F5刷新的，就维护store
                initStore()
            }
            next();
        } else {
            clearStorage();
            next('/login');
        }
        // isLogin ? next : ;
    }
    // next();
})
export default router